package ru.compscicenter.uno;

import ru.compscicenter.uno.actor.Actor;
import ru.compscicenter.uno.actor.bot.Bot;
import ru.compscicenter.uno.actor.human.Human;
import ru.compscicenter.uno.controller.AController;
import ru.compscicenter.uno.controller.ClientController;
import ru.compscicenter.uno.controller.ServerController;
import ru.compscicenter.uno.log.Logger;
import ru.compscicenter.uno.view.ConsoleViewer;
import ru.compscicenter.uno.view.SwingViewer;
import ru.compscicenter.uno.view.View;
import ru.compscicenter.uno.wizard.WizardConsole;
import ru.compscicenter.uno.wizard.WizardGUI;

import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Павел
 * Date: 15.10.11
 * Time: 1:48
 */
public class Main {
    public static void main(String[] args) {
        new Logger(Logger.Mode.ACTION, Logger.DEFAULT_FILE);
        Logger.write("\n", Logger.Mode.ACTION);
        Logger.write("APPLICATION STARTED", Logger.Mode.ACTION);
        ArrayList<String> argList = new ArrayList<String>();

        for (String str : args) {
            argList.add(str);
        }

        if (argList.contains("gui")) {
            WizardGUI wizard = new WizardGUI();
            wizard.start();
        } else if (argList.contains("test")) {
            final WizardConsole wizard = new WizardConsole();
            int i = 0;
            final int n;
            while (!argList.get(i).equals("test")) {
                i++;
            }
            try {
                n = Integer.parseInt(argList.get(i + 1));
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        wizard.startServer("Server", n - 1);
                    }
                }).start();

                for (int j = 0; j != n - 1; j++) {
                    final int k = j;
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            wizard.startClient("client_" + k, "127.0.0.1:3129");
                        }
                    }).start();
                }
            } catch (NumberFormatException e) {
                System.out.println("Wrong parameter: \"test n\", where n is number of players");
            } catch (IndexOutOfBoundsException e) {
                System.out.println("Wrong parameter: \"test n\", where n is number of players. Missing n.");
            }
        } else {
            WizardConsole wizard = new WizardConsole();
            wizard.start();
        }
    }
/*
	public static void main(String[] args) {
		View viewer = null;
		AController controller = null;
		Actor actor = null;

		try {
			ArrayList<String> arguments = new ArrayList<>(args.length);
			for (String str : args) {
				arguments.add(str);
			}

			if (arguments.contains("help")) {
				printHelp();
			}

			boolean isBroadcast = arguments.contains("broadcast");
			if (arguments.contains("0") || arguments.contains("client")) {
				controller = new ClientController(isBroadcast);
				if (arguments.contains("bot")) {
					actor = new Bot(0, controller);
				} else {
					actor = new Human();
				}
			} else if (arguments.contains("1")) {
				controller = new ServerController(isBroadcast, "server");
                if (arguments.contains("bot")) {
                    actor = new Bot(1, controller);
                } else {
                    actor = new Human();
                }
			} else {
				throw new IllegalArgumentException("Invalid usage: server or client type required");
			}
			controller.setDaemon(true);
			controller.start();

			if (arguments.contains("console")) {
				viewer = new ConsoleViewer(controller, actor);
			} else {
				viewer = new SwingViewer(controller);
			}
		} catch (IllegalArgumentException e) {
			System.err.println("Error: " + e.getMessage());
			printHelp();
		}

		viewer.run();
	}

	private static void printHelp() {
		System.out.println(
				"command line options:\n" +
						"\t\t'0' or 'client' - client mode\n" +
						"\t\t'1' or 'server' - server mode\n" +
						"\t\t'console' - UI will turn off, game will run in console\n" +
						"\t\t'bot' - player will be AI (works only on console client)\n" +
						"\t\t'broadcast' - broadcasing server info (if not set, IP must be set manually)"
		);
	}
	*/
}
